package cn.topcodes.tcsf.admin.commons.api.controller.admin;

import cn.topcodes.tcsf.admin.commons.api.controller.generic.GenericApiController;
import cn.topcodes.tcsf.admin.commons.domain.IdEntity;
import cn.topcodes.tcsf.admin.commons.domain.Idable;
import cn.topcodes.tcsf.admin.commons.generic.ISessionService;
import cn.topcodes.tcsf.admin.commons.generic.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 管理页面跳转抽象控制器
 * @author Unicorn
 * @date 2017/4/26
 */
public abstract class AdminController<T extends IdEntity, V, F extends Idable>extends GenericApiController<T,V,F> {

    @Autowired
    private IUserService userService;

    @Autowired
    private ISessionService sessionService;

    /*管理主页*/
    @RequestMapping("")
    public String index(HttpServletRequest request, Model model) {
        paramToModel(request, model);
        return this.getPageFolder() + "/index";
    }

    /*创建页面*/
    @RequestMapping(value="create",method= RequestMethod.GET)
    public String create(HttpServletRequest request, Model model) {
        paramToModel(request, model);
        return this.getPageFolder() + "/create";
    }

    /*修改页面*/
    @RequestMapping(value="update",method=RequestMethod.GET)
    public String update(@RequestParam Long id, HttpServletRequest request, Model model) {
        paramToModel(request, model);
        T entity = this.getService().findOne(id);
        model.addAttribute("entity",entity);
        return this.getPageFolder() + "/update";
    }

    @RequestMapping(value="detail",method=RequestMethod.GET)
    public String detail(@RequestParam Long id, HttpServletRequest request, Model model) {
        T entity = this.getService().findOne(id);
        V vo = this.entity2Vo(entity,request);
        model.addAttribute("entity",vo);
        return this.getPageFolder() + "/detail";
    }

    @Override
    protected boolean validateOperation(String password) {
        Long userId = sessionService.getCurrentUserId();
        return this.userService.isPwdEqual(password, userId);
    }

    protected abstract String getPageFolder();

    /**
     * 请求参数赋入model
     * @param request
     * @param model
     */
    private void paramToModel(HttpServletRequest request, Model model) {
        Map<String, String[]> params = request.getParameterMap();
        for(Map.Entry<String,String[]> param : params.entrySet()) {
            String[] values = param.getValue();
            if(values != null) {
                if(values.length == 1) {
                    model.addAttribute(param.getKey(), values[0]);
                }else {
                    model.addAttribute(param.getKey(), param.getValue());
                }
            }
        }
    }
}
